﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Media.Animation;

namespace App16._3
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        public void OnLoaded(object sender, RoutedEventArgs args)
        {
            ThreeSegDoubleAnimation animation = new ThreeSegDoubleAnimation();
            animation.Range = 100;
            animation.Duration = new TimeSpan(0, 0, 10);

            Storyboard.SetTargetName(animation, "ellipse");
            Storyboard.SetTargetProperty(animation, 
                new PropertyPath(EllipseGeometry.RadiusYProperty));

            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(animation);
            storyboard.Begin(this);
        }
    }

    public class ThreeSegDoubleAnimation : DoubleAnimationUsingKeyFrames
    {
        public ThreeSegDoubleAnimation()
            : base()
        {
            KeyFrames.Add(_frame1);
            KeyFrames.Add(_frame2);
            KeyFrames.Add(_frame3);
            KeyFrames.Add(_frame4);
        }

        protected override void OnPropertyChanged(
            DependencyPropertyChangedEventArgs args)
        {
            if (args.Property == Timeline.DurationProperty)
            {
                Nullable<Duration> duration = args.NewValue as Nullable<Duration>;
                if (duration.HasValue)
                {
                    _frame1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromTicks(0));
                    _frame2.KeyTime = KeyTime.FromTimeSpan(
                        TimeSpan.FromTicks(duration.Value.TimeSpan.Ticks / 3));
                    _frame3.KeyTime = KeyTime.FromTimeSpan(
                        TimeSpan.FromTicks(duration.Value.TimeSpan.Ticks * 2 / 3));
                    _frame4.KeyTime = KeyTime.FromTimeSpan(
                        TimeSpan.FromTicks(duration.Value.TimeSpan.Ticks));
                }
            }

            base.OnPropertyChanged(args);
        }

        public static void OnRangeChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs args)
        {
            ThreeSegDoubleAnimation animation = obj as ThreeSegDoubleAnimation;
            if (animation == null)
                return;

            Nullable<Double> range = args.NewValue as Nullable<Double>;
            if (!range.HasValue)
                return;

            animation._frame2.Value = range.Value / 10;
            animation._frame3.Value = range.Value * 9 / 10;
            animation._frame4.Value = range.Value;
        }

        private LinearDoubleKeyFrame _frame1 = new LinearDoubleKeyFrame(0);
        private LinearDoubleKeyFrame _frame2 = new LinearDoubleKeyFrame(0);
        private LinearDoubleKeyFrame _frame3 = new LinearDoubleKeyFrame(0);
        private LinearDoubleKeyFrame _frame4 = new LinearDoubleKeyFrame(0);

        public Double Range
        {
            get { return (Double)GetValue(RangeProperty); }
            set { SetValue(RangeProperty, value); }
        }

        public static readonly DependencyProperty RangeProperty =
            DependencyProperty.Register("Range", typeof(Double), 
            typeof(ThreeSegDoubleAnimation),
            new PropertyMetadata(Double.Parse("0"), new 
                PropertyChangedCallback(OnRangeChanged)));
    }
}
